<?php
class My_Timer {
	private $markers;
	
	public function __construct() {
		$this->markers = array ( );
	}
	
	public function start() {
		$this->setMarker ( 'Start' );
	}
	
	public function stop() {
		$this->setMarker ( 'Stop' );
	}
	
	public function setMarker($name) {
		$this->markers [$name] = $this->_getMicrotime ();
	}
	
	public function _getMicrotime() {
		$microtime = explode ( ' ', microtime () );
		return $microtime [1] . substr ( $microtime [0], 1 );
	}
	
	public function timeElapsed($start = 'Start',$end = 'Stop') {
		if ($end == 'Stop' && ! isset ( $this->markers ['Stop'] )) {
			$this->markers ['Stop'] = $this->_getMicrotime ();
		}
		
		if (extension_loaded ( 'bcmath' )) {
			return bcsub ( $this->markers [$end], $this->markers [$start], 6 );
		} else {
			return substr ( ($this->markers [$end] - $this->markers [$start]), "", 8 );
		}
	}
	
	public function display() {
		return $this->timeElapsed ();
	}
}
?>
